import Vue from 'vue';
import {} from '../base/observer.js';
var mpheader = require("../components/mpheader.js");
var myVerification = require("../components/verification.js");
var dateTime = require("../components/dateTime.js");
var infoVue;
var djs = 60;

// 身份证正则表达式
var idRegExp = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;

// 设置信息填写框高度
function setInfoHeight() {
    var infoPrentHeigth = (window.innerHeight - 46) + "px";
    $("#app_info>.page__bd").css("height", infoPrentHeigth);
}
// 
function createInfoVue(commonData) {
    setInfoHeight();
    var mheder1 = mpheader.getMPheader({
        "titleText": "申请小通秒贷"
    }, function() {
        commonData.step = 2;
    });
    infoVue = new Vue({
        el: "#app_info",
        data: {
            commonData: commonData,
            name: "",
            istsName: false,
            userId: "",
            istsSFZ: false,
            userIdFormat: "",
            cardId: "",
            istsCardId: false,
            cardIdFormat: "",
            xykId: "",
            istsxyk: false,
            xykIdFormat: "",
            cvn2: "",
            istscvn2: false,
            timeNum: "",
            istsTimeNum: false,
            phoneNo: "",
            istsPhoneNo: false,
            phoneFormat: "",
            vcode: "",
            istsVcode: false,
            dxbtnmsg: "获取验证码"
        },
        computed: {

        },
        components: {
            "mpheader": mheder1
        },
        methods: {
            "submitInfo": function(event) {
                if ($(event.target).hasClass("weui-btn_disabled")) {
                    return false;
                } else {
                    $.publish("showConfirmDailog");
                }

            },
            "nameInput": function() {
                
                var isn = iscnName();
                if (isn) {
                    this.istsName = false;
                }
                disabledSumbit();
            },
            "nameInputChange": function() {
                this.name = this.name.replace(/[^\u4e00-\u9fa5]/g, "");
                var isn = iscnName();
                if (isn) {
                    this.istsName = false;
                } else {
                    this.istsName = true;
                }

            },
            "userIdInput": function(event) {
                this.userId = this.userId.replace(/\*/g, "X").replace(/[^0-9|X|x]/g, "");;
                userIdFormat(event);
                if (idRegExp.test(this.userId)) {
                    this.istsSFZ = false;
                }
                disabledSumbit();
            },
            "userIdChange": function() {

                if (!idRegExp.test(this.userId.replace(/\s/g, ""))) {
                    this.istsSFZ = true;
                }
                if (this.userId.length < 14) {
                    this.userId = this.userId.replace(/(\d{6})/, "$1  ");
                } else {
                    this.userId = this.userId.replace(/(\d{6})(\d{8})/, "$1  $2  ");
                }

            },
            "cardIdInput": function(event) {
                if (this.cardId.search(/\D/) != -1) {
                    this.cardId = this.cardId.replace(/\D/g, "");
                }
                infoVue.cardIdFormat = creditCardFormat(event);
                if (this.cardId.length > 12 && this.cardId.length < 20) {
                    this.istsCardId = false;
                }

                disabledSumbit();
            },
            "cardIdInputChange": function() {
                if (this.cardId.length < 13 || this.cardId.length > 19) {
                    this.istsCardId = true;
                }
                if (this.cardId.length > 4 && this.cardId.length < 9) {
                    this.cardId = this.cardId.replace(/(\d{4})/, "$1  ");
                } else if (this.cardId.length > 8 && this.cardId.length < 13) {
                    this.cardId = this.cardId.replace(/(\d{4})(\d{4})/, "$1  $2  ");
                } else if (this.cardId.length > 12 && this.cardId.length < 17) {
                    this.cardId = this.cardId.replace(/(\d{4})(\d{4})(\d{4})/, "$1  $2  $3  ");
                } else if (this.cardId.length > 16) {
                    this.cardId = this.cardId.replace(/(\d{4})(\d{4})(\d{4})(\d{4})/, "$1  $2  $3  $4 ");
                }
            },
            "xykIdInput": function(event) {
                if (this.xykId.search(/\D/) != -1) {
                    this.xykId = this.xykId.replace(/\D/g, "");
                }
                infoVue.xykIdFormat = creditCardFormat(event);
                if (this.xykId.length > 12 && this.xykId.length < 18) {
                    this.istsxyk = false;
                }
                disabledSumbit();
            },
            "xykInputChange": function() {
                if (this.xykId.length < 13 || this.xykId.length > 17) {
                    this.istsxyk = true;
                }
                if (this.xykId.length > 4 && this.xykId.length < 9) {
                    this.xykId = this.xykId.replace(/(\d{4})/, "$1  ");
                } else if (this.xykId.length > 8 && this.xykId.length < 13) {
                    this.xykId = this.xykId.replace(/(\d{4})(\d{4})/, "$1  $2  ");
                } else if (this.xykId.length > 12 && this.xykId.length < 17) {
                    this.xykId = this.xykId.replace(/(\d{4})(\d{4})(\d{4})/, "$1  $2  $3  ");
                } else if (this.xykId.length > 16) {
                    this.xykId = this.xykId.replace(/(\d{4})(\d{4})(\d{4})(\d{4})/, "$1  $2  $3  $4 ");
                }
            },
            "cvn2input": function() {
                if (this.cvn2.search(/\D/) != -1) {
                    this.cvn2 = this.cvn2.replace(/\D/g, "");
                }
                if (this.cvn2.length == 3) {
                    this.istscvn2 = false;
                }
                disabledSumbit();
            },
            "cvn2inputChange": function() {
                if (this.cvn2.length == 3) {
                    this.istscvn2 = false;
                } else {
                    this.istscvn2 = true;
                }
            },
            "timeNumInput": function() {
                if (this.timeNum.search(/\D/) != -1) {
                    this.timeNum = this.timeNum.replace(/\D/g, "");
                }
                var istn = isTimeNum(this.timeNum);
                if (istn) {
                    this.istsTimeNum = false;
                }
                disabledSumbit();
            },
            "timeNumInputChange": function() {
                var istn = isTimeNum(this.timeNum);
                if (istn) {
                    this.istsTimeNum = false;
                } else {
                    this.istsTimeNum = true;
                }
            },
            "phoneInput": function(event) {
                if (this.phoneNo.search(/\D/) != -1) {
                    this.phoneNo = this.phoneNo.replace(/\D/g, "");
                }
                infoVue.phoneFormat = userPhoneFormat(event);
                if (this.phoneNo.length == 11) {
                    if (djs == 60) {
                        $("#getVcode_btn").removeClass("disabled");
                    }
                    this.istsPhoneNo = false;
                } else {
                    $("#getVcode_btn").addClass("disabled");
                }
                disabledSumbit();
            },
            "phoneInputChange": function() {

                if (this.phoneNo.length != 11) {
                    this.istsPhoneNo = true;
                } else {
                    this.istsPhoneNo = false;
                }
                if (this.istsPhoneNo.length > 3 && this.istsPhoneNo.length < 7) {
                    this.istsPhoneNo = this.istsPhoneNo.replace(/(\d{3})/, "$1  ");
                } else if (this.istsPhoneNo.length > 8) {
                    this.istsPhoneNo = this.istsPhoneNo.replace(/(\d{3})(\d{4})/, "$1  $2  ");
                }
            },
            "vcodeInput": function() {
                if (this.vcode.search(/\D/) != -1) {
                    this.vcode = this.vcode.replace(/\D/g, "");
                }
                if (this.vcode.length == 4) {
                    this.istsVcode = false;
                }
                disabledSumbit();
            },
            "vcodeInputChange": function() {
                if (this.vcode.length == 4) {
                    this.istsVcode = false;
                } else {
                    this.istsVcode = true;
                }
            },
            "inputfocus": function(event) {
                $(event.target).parent().parent().find("p.input_format").show();
            },
            "inputblur": function(event) {
                $(event.target).parent().parent().find("p.input_format").hide();
            },
            "getVcode": function() {
                var $getVcodeBtn = $("#getVcode_btn");

                if ($getVcodeBtn.hasClass("disabled")) {
                    return;
                } else {
                    $getVcodeBtn.addClass("disabled");
                    infoVue.dxbtnmsg = "重新获取(" + djs + ")";
                    var gvcHande = setInterval(function() {
                        djs--;
                        infoVue.dxbtnmsg = "重新获取(" + djs + ")";
                        if (djs == 0) {
                            infoVue.dxbtnmsg = "重新获取";
                            $getVcodeBtn.removeClass("disabled");
                            clearInterval(gvcHande);
                            djs = 60;
                        }
                    }, 1000);
                    $.publish("togetvcode");
                }

            }
        }
    });

    return infoVue;
}

// 身份证格式提示
function userIdFormat(event) {
    var thisVal = $(event.target).val();
    thisVal = thisVal.replace(/[^0-9|X|x]/g, "");
    thisVal = thisVal.replace("x", "X");

    var formatVal = thisVal;
    if (thisVal.length > 6 && thisVal.length < 14) {
        formatVal = thisVal.replace(/(\d{6})/, "$1  ");
    } else if (thisVal.length > 14) {
        formatVal = thisVal.replace(/(\d{6})(\d{8})/, "$1  $2  ");
    }

    infoVue.userIdFormat = formatVal;
    return formatVal;
}

// 信用卡输入实时格式化提示
function creditCardFormat(event) {
    var thisVal = $(event.target).val();
    thisVal = thisVal.replace(/\D/g, "");
    var formatVal = thisVal;


    if (thisVal.length > 4 && thisVal.length < 9) {
        formatVal = thisVal.replace(/(\d{4})/, "$1  ");
    } else if (thisVal.length > 8 && thisVal.length < 13) {
        formatVal = thisVal.replace(/(\d{4})(\d{4})/, "$1  $2  ");
    } else if (thisVal.length > 12 && thisVal.length < 17) {
        formatVal = thisVal.replace(/(\d{4})(\d{4})(\d{4})/, "$1  $2  $3  ");
    } else if (thisVal.length > 16) {
        formatVal = thisVal.replace(/(\d{4})(\d{4})(\d{4})(\d{4})/, "$1  $2  $3  $4 ");
    }

    return formatVal;
}


// 手机格式化提示
function userPhoneFormat(event) {
    var thisVal = $(event.target).val();
    var phoneFormat = thisVal.replace(/\D/g, "");

    if (thisVal.length > 3 && thisVal.length < 7) {
        phoneFormat = thisVal.replace(/(\d{3})/, "$1  ");
    } else if (thisVal.length > 8) {
        phoneFormat = thisVal.replace(/(\d{3})(\d{4})/, "$1  $2  ");
    }

    return phoneFormat;
}


// 判断是否2-8个中文
function iscnName() {
    var pdjieguo = false;
    var iscnName = myVerification.isCnName(infoVue.name);
    if (iscnName && infoVue.name.length > 1 && infoVue.name.length < 9) {
        pdjieguo = true;
    }

    return pdjieguo;
}

// 验证四位时间 2016年04月 0416
function isTimeNum(timeNum) {
    var istNum = true;
    if (timeNum.length < 4 || parseInt(timeNum.substr(0, 2)) > 12 || parseInt(timeNum.substr(0, 2)) < 1) {
        istNum = false;
    } else {
        //验证是否 大于现在
        istNum = dateTime.isNextYearMonth(timeNum.substr(2, 2), timeNum.substr(0, 2));
    }


    return istNum;
}


// 是否禁止提交
function disabledSumbit() {
    var iscnN = iscnName();
    var issfz = idRegExp.test(infoVue.userId.replace(/\s/g,""));
    var istn = isTimeNum(infoVue.timeNum);
    var iscardId = false;
    var isxyk = false;
    if (infoVue.cardId.replace(/\s/g,"").length > 12 && infoVue.cardId.replace(/\s/g,"").length < 20) {
        iscardId = true;
    }
    if (infoVue.xykId.replace(/\s/g,"").length > 12 && infoVue.xykId.replace(/\s/g,"").length < 17) {
        isxyk = true;
    }

    var $submitInfoBtn = $("#submitInfo_btn");
    if (iscnN && issfz && issfz && iscardId && isxyk && infoVue.cvn2.length == 3 && istn && infoVue.phoneNo.replace(/\s/g,"").length == 11 && infoVue.vcode.length == 4) {
        $submitInfoBtn.removeClass("weui-btn_disabled");
    } else {
        $submitInfoBtn.addClass("weui-btn_disabled");
    }
}

module.exports = {
    createInfoVue: createInfoVue
}